package dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;

import dao.AddressDao;
import domain.Address;
import domain.Order;
import util.DataSourceUtils;

public class AddressDaoImpl implements AddressDao {

	@Override
	public List<Address> findAddressByUser(int userId, int page, int limit) throws SQLException {
		String sql = "select addresses.* from addresses where user_id=? limit ?,?";
		QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
		return runner.query(sql, new ResultSetHandler<List<Address>> () {

			@Override
			public List<Address> handle(ResultSet rs) throws SQLException {
				List<Address> addresses = new ArrayList<Address>();
				
				while (rs.next()) {
					Address address = new Address();
					
					address.setId(rs.getInt("addresses.id"));
					address.setName(rs.getString("addresses.name"));
					address.setTelephone(rs.getString("addresses.telephone"));
					address.setAddress(rs.getString("addresses.address"));
					
					addresses.add(address);
				}
				
				return addresses;
			}
			
		}, userId, (page - 1) * limit, limit);
	}

	@Override
	public boolean createAddress(Address address) throws SQLException {
		String sql = "insert into addresses(name,telephone,address,user_id) values(?,?,?,?)";
		QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
		try {
			runner.update(sql, address.getName(), address.getTelephone(), address.getAddress(), address.getUser().getId());
			return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public boolean updateAddress(Address address) throws SQLException {
		String sql = "update addresses set name=?,telephone=?,address=? where id=?";
		QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
		try {
			runner.update(sql, address.getName(), address.getTelephone(), address.getAddress(), address.getId());
			return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public boolean deleteAddressById(int id) throws SQLException {
		String sql = "delete from addresses where id=?";
		QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
		try {
			runner.update(sql, id);
			return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public Address findAddressById(int id) throws SQLException {
		String sql = "select addresses.* from addresses where id=?";
		QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
		return runner.query(sql, new ResultSetHandler<Address> () {

			@Override
			public Address handle(ResultSet rs) throws SQLException {
				Address address = new Address();
				
				while (rs.next()) {
					address.setId(rs.getInt("addresses.id"));
					address.setName(rs.getString("addresses.name"));
					address.setTelephone(rs.getString("addresses.telephone"));
					address.setAddress(rs.getString("addresses.address"));
				}
				
				return address;
			}
			
		}, id);
	}

	
}
